package com.cn.goodsadmin.service;

import com.cn.goodsadmin.common.PageQueryResult;
import com.cn.goodsadmin.common.observes.MessageConfig;
import com.cn.goodsadmin.common.observes.MessageManager;
import com.cn.goodsadmin.dao.DestDTO;
import com.cn.goodsadmin.observer.UpdateOrderStatusMessage;
import com.cn.goodsadmin.po.Logistics;
import com.cn.goodsadmin.po.PanicLog;
import com.cn.goodsadmin.util.TimeUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("PanicLogService")
public class PanicLogService extends BaseService{

    /**
     * 获取所有的抢购信息
     * @param pageindex
     * @param pagesize
     * @param type
     * @param appid
     * @param state
     * @return
     * @throws Exception
     */
    public PageQueryResult<DestDTO> findAllPanicLogPage(int pageindex, int pagesize, Integer type, int appid, int state)throws Exception{
            return panicLogDao.findPanicLog(pageindex,pagesize,appid,type,state);
    }


    public void  sendPanicOrder(String pid,int appid,String logisticsid,String logisticsname)throws Exception{
        List<PanicLog> panicLogs=panicLogDao.findPanicLogByPid(pid,null,appid);
        if(panicLogs!=null&&panicLogs.size()>0) {
            PanicLog panicLog = panicLogs.get(0);
            int oldstatuts = (int) panicLog.getcStatus();
            if (oldstatuts == 1) {
                //检查订单号是否存在
                Logistics logistics=logisticsDao.findLogisticsByLid(logisticsid);
                if(logistics==null) {
                    //入库添加物流
                    logistics = new Logistics();
                    logistics.setcLogisticsid(logisticsid);
                    logistics.setcName(logisticsname);
                    logisticsDao.save(logistics);
                }else if(!logisticsname.equals(logistics.getcName())){
                    //更新
                    logistics.setcName(logisticsname);
                    logisticsDao.update(logistics);
                }
                //修改订单
                panicLog.setcStatus((byte) 2);
                panicLog.setcLogisticsid(logisticsid);
                panicLog.setcMtime(TimeUtil.getCurrTimeHm());
                panicLogDao.update(panicLog);
                UpdateOrderStatusMessage message = new UpdateOrderStatusMessage(MessageConfig.UPDATEORDERSTATUTS, panicLog);
                MessageManager.getInstanceMessage().putMessage(message);
            }
        }

    }
}
